
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        titleArr: ['代金券', '满减券', '商品券', '折扣券'],
        orderArray: ['一小时', '三小时', '一天', '两天', '三天'],
        orderIndex: 0,
        numArray: ['最多使用一张', '无限制'],
        numIndex: 0,
        dateString: '',
        info: {
            type: '',
            name: '',
            validityLowerBound: '',
            validityUpperBound: '',
            value: '',
            condition: '',
            date: '',
            timezoneLower: '',
            timezoneUpper: '',
            minimum: '',
            amount: '',
            reservation: ''
        },
        more: '',//可以为空
        hideDel: false,
        showData: false
    },
    delTicket(){
      wx.showModal({
        title: '提示',
        content: '确认删除？',
        success: (res) => {
          if(res.confirm){
            app.globalData.post({
              url: '/couponPool/delCouponPool',
              data: {
                couponPoolIds: JSON.stringify([this.data.info.couponPoolId])
              },
              success: () => {
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.init({
                  type: 't' + this.data.info.type
                })
                wx.navigateBack({
                  
                })
              }
            })
          }
        }
      })
    },
    navDate(){
        wx.navigateTo({
            url: `/pages/dateChoose/dateChoose?date=${this.data.info.date}`,
        })
    },
    iMore(e){
        this.setData({
          more: e.detail.value
        })
    },
    orderPickerChange(e) {
        let index = e.detail.value
        this.data.info.reservation = this.data.orderArray[index]
        
        this.setData({
            info: this.data.info,
            orderIndex: index
        })
    },
    numPickerChange(e) {
        let index = e.detail.value
        this.data.info.amount = this.data.numArray[index]
        
        this.setData({
            info: this.data.info,
            numIndex: index
        })
    },
    iName(e) {
        this.data.info.name = e.detail.value
        this.setData({
            info: this.data.info
        })
    },
    pvlb(e) {
        this.data.info.validityLowerBound = e.detail.value
        this.setData({
            info: this.data.info
        })
    },
    pvub(e) {
        this.data.info.validityUpperBound = e.detail.value
        this.setData({
            info: this.data.info
        })
    },
    ptimezoneLowerBound(e) {
        this.data.info.timezoneLower = e.detail.value
        this.setData({
            info: this.data.info
        })
    },
    ptimezoneUpperBound(e) {
        this.data.info.timezoneUpper = e.detail.value
        this.setData({
            info: this.data.info
        })
    },
    iMinimum(e) {
        this.data.info.minimum = e.detail.value
        this.setData({
            info: this.data.info
        })
    },
    iMaximum(e) {
        this.data.info.maximum = e.detail.value
        this.setData({
            info: this.data.info
        })
    },
    create() {
        if (Object.values(this.data.info).filter(val => String(val).trim() === '').length){
            wx.showModal({
                title: '提示',
                content: '尚有信息未填写',
                showCancel: false
            })
            return
        }
        let result = {}
        let info = this.data.info
        result.type = info.type
        result.name = info.name
        result.validityLowerBound = info.validityLowerBound
        result.validityUpperBound = info.validityUpperBound
        result.restrictRules = {
            date: info.date,
            timezoneLower: info.timezoneLower,
            timezoneUpper: info.timezoneUpper,
            minimum: info.minimum * 100,
            amount: info.amount,
            reservation: info.reservation,
            more: this.data.more
        }
        if(info.type == 1 || info.type == 2){
            result.favorableRules = JSON.stringify({condition: info.condition * 100, value: info.value * 100})
        }
        if(info.type == 3){
            result.favorableRules = JSON.stringify({ condition: info.condition, value: info.value})
        }
        if(info.type == 4){
            result.favorableRules = JSON.stringify({ condition: info.condition, value: info.value * 10, maximum: info.maximum * 100})
        }
        result.restrictRules = JSON.stringify(result.restrictRules)
        console.log(result)
        app.globalData.post({
            url: '/couponPool/create',
            data: result,
            success: (data) => {
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.init({
                    type: 't' + this.data.info.type
                })
                wx.navigateBack({
                    
                })
            }
        })
    },
    iValCon4(e){
        this.data.info.value = e.detail.value
        this.data.info.condition = 0
        this.setData({
            info: this.data.info
        })
    },
    iValCon3(e){
        this.data.info.value = e.detail.value
        this.data.info.condition = 0
        this.setData({
            info: this.data.info
        })
    },
    iVal2(e){
        this.data.info.value = e.detail.value
        this.setData({
            info: this.data.info
        })
    },
    iCon2(e){
        this.data.info.condition = e.detail.value
        this.setData({
            info: this.data.info
        })
    },
    iValCon1(e){
        this.data.info.value = e.detail.value
        this.data.info.condition = +e.detail.value + .01

        this.setData({
            info: this.data.info
        })
    },  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let data = this.data
        let info = data.info
        console.log(options)
        // let deliver = options.deliver
        let type = options.type

        //如果传入obj 说明页面变成展示作用
        if(options.obj){
          data.hideDel = options.hideDel ? options.hideDel : false
          this.setData({
            hideDel: data.hideDel,
            showData: true
          })
          info = JSON.parse(options.obj)
          info.couponPoolId = info.couponPoolId ? info.couponPoolId : info.id
          info.favorableRules = JSON.parse(info.favorableRules)
          info.restrictRules = JSON.parse(info.restrictRules)
          Object.assign(info, info.restrictRules, info.favorableRules)
          let orderIndex = data.orderArray.findIndex(val => {
            return val.includes(info.reservation)
          })
          let numIndex = data.numArray.findIndex(val => {
            return val.includes(info.amount)
          })
          this.setData({
            orderIndex,
            numIndex
          })
          if (info.type == 1 || info.type == 2) {     
            info.condition = info.condition / 100
            info.value = info.value / 100
          }
          if (info.type == 4) {
            info.condition = info.condition
            info.value = info.value / 10
            info.maximum = info.maximum / 100
          }
        }
        else{
          //没有传入obj就初始化数据
          info.type = type
          info.reservation = data.orderArray[data.orderIndex]
          info.amount = data.numArray[data.numIndex]
        }
        
        this.setData({
            info: info
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    setDate(date){
        this.data.info.date = date
        this.setData({
            info: this.data.info
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        let dateString = this.data.dateString
        let date = this.data.info.date
        if (date == '1,2,3,4,5,6,7') {
            dateString = '全天'
        }
        else if (date == '1,2,3,4,5') {
            dateString = '工作日'
        }
        else if (date) {
            dateString = date.split(',')
            dateString = dateString.map(val => {
                if (val == '1') {
                    return '周一'
                }
                if (val == '2') {
                    return '周二'
                }
                if (val == '3') {
                    return '周三'
                }
                if (val == '4') {
                    return '周四'
                }
                if (val == '5') {
                    return '周五'
                }
                if (val == '6') {
                    return '周六'
                }
                if (val == '7') {
                    return '周日'
                }
            })
                .join('、')
        }
        this.setData({
            dateString: dateString ? (dateString + '可用') : ''
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})